#include("../../layout/header.html")
#@layout()
#define content()
<style>
    body .tox-tinymce-aux {
        z-index: 19892000 !important;
    }
</style>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字:</label>
                        <div class="layui-input-inline">
                            <input name="searchValue" class="layui-input" placeholder="输入标题、创建人"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">类型:</label>
                        <div class="layui-input-inline">
                            <select id="type1" name="type">
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">状态:</label>
                        <div id="state1" class="layui-input-block"></div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="dataTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="dataTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/javascript" src="/static/libs/tinymce/tinymce.min.js"></script>
<!-- 表单弹窗 -->
<script type="text/html" id="dataEditDialog">
    <form id="dataEditForm" lay-filter="dataEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">标题:</label>
            <div class="layui-input-block">

                <input name="title" placeholder="请输入标题" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">

                <label class="layui-form-label layui-form-required">类型:</label>
                <div class="layui-input-block">
                    <select id="type" name="type" lay-filter="title">
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label layui-form-required">状态:</label>
                <div class="layui-input-block">
                    <div id="state" lay-filter="state"></div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">内容:</label>
            <div class="layui-input-block">
                <textarea id="demoEditor"></textarea>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="dataEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree', 'formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var formX = layui.formX;
        var admin = layui.admin;

        var noticeType = [],noticeState = [];
        admin.req('/sys/sysDictData/get', {type: 'noticeType,state'}, function (d) {
            if (0 === d.code) {
                noticeType = d.data.noticeType;
                noticeState = d.data.state;

                formX.renderSelect({
                    elem: '#type1',
                    data: noticeType,
                    name: 'name',
                    value: 'value',
                    hint: '请选择公告类型'
                });

                formX.renderRadio({
                    elem: '#state1',
                    data: noticeState,
                    name: 'state',
                    title:'name',
                    value: 'value',
                })
            }
        })

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#dataTable',
            url: '/sys/sysNotice/list',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'title', title: '标题', sort: true},
                {field: 'typeStr', title: '类型', sort: true},
                {field: 'stateStr', title: '状态'},
                {field: 'createdBy', title: '创建人'},
                {
                    field: 'createdTime', title: '创建时间', sort: true
                },
                {title: '操作', toolbar: '#dataTbBar', align: 'center', width: 200, minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(dataTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.popupRight({
                type: 1,
                area: ['97%', '94%'],
                title: (mData ? '修改' : '添加') + '公告',
                content: $('#dataEditDialog').html(),
                success: function (layero, dIndex) {
                    // 渲染搜索栏中的性别select
                    formX.renderSelect({
                        elem: '#type',
                        data: noticeType,
                        name: 'name',
                        value: 'value',
                        initValue: mData ? mData.type : "",
                        hint: '请选择公告类型'
                    });

                    formX.renderRadio({
                        elem: '#state',
                        data: noticeState,
                        name: 'state',
                        title:'name',
                        value: 'value',
                        initValue: mData ? mData.state : ""
                    })



                    tinymce.init({
                        selector: '#demoEditor',
                        height: 525,
                        branding: false,
                        language: 'zh_CN',
                        plugins: 'code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount',
                        toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor pagebreak codesample | ltr rtl',
                        toolbar_drawer: 'sliding',
                        images_upload_url: '../../../json/tinymce-upload-ok.json',
                        file_picker_types: 'media',
                        file_picker_callback: function (callback, value, meta) {
                            layer.msg('演示环境不允许上传', {anim: 6});
                        },
                        init_instance_callback: function (editor) {
                            if(mData){
                                tinymce.get('demoEditor').setContent(mData.content);
                            }
                        }
                    });

                    // 回显表单数据
                    form.val('dataEditForm', mData);

                    // 表单提交事件
                    form.on('submit(dataEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);

                        data.field.content = tinymce.get('demoEditor').getContent();

                        admin.post('/sys/sysNotice/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        });
                        return false;
                    });
                },
                end: function () {
                    // 销毁编辑器
                    tinymce.get('demoEditor').destroy(false);
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/sys/sysNotice/del', {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        }

    });
</script>
</body>
#end